/*
 * ObjectLine.java
 *
 * Created on December 13, 2007, 12:10 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package editor;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

/**
 *
 * @author Martin Demin, Eduard Benes
 */
public final class ObjectLine implements Object {
    
    Point2D start;
    Point2D end;
    private boolean selected;
    int draggedPoint;
    
    private float scaleX;
    private float scaleY;
    private ObjectInfo info;
    
    
    
    /** Creates a new instance of ObjectPolygon */
    public ObjectLine() {
        start = null;
        end = null;
        selected = false;
        scaleX = 1;
        scaleY = 1;
        draggedPoint = -1;
        info = null;
        
    }

    /**
     * Add point to the object, coordinates are in meters
     */
    public void addPoint(Point2D p) {
        if(start == null)
        {
            start = p;
            return;
        }
        end = p;
    }
    
    /**
     * Redraw this object
     */
    public void draw(Graphics g) {
        
        if(selected) 
            g.setColor(Color.RED);
        else
            g.setColor(Color.WHITE);
        
        if(start != null)
            g.drawRect((int)(start.getX()*scaleX-3), (int)(start.getY()*scaleY-3), 6 , 6);
        if(end != null && start != null)
        {
            g.drawRect((int)(end.getX()*scaleX-3), (int)(end.getY()*scaleY-3), 6 , 6);
            g.drawLine((int)(start.getX()*scaleX),(int)(start.getY()*scaleY),
                    (int)(end.getX()*scaleX),(int)(end.getY()*scaleY) );
        }   
        
    }

    
    /**
     * Get the type of the object
     */
    public int getType() {
        return 1; // polygon
    }
    
    /**
     * Set scaling of the object pixel -> meters transformation
     */
    public void setScale(float scaleX, float scaleY) {
        this.scaleX = scaleX;
        this.scaleY = scaleY;
    }
    
    /**
     * Unselect the current object
     */
    public void unselect() {
        selected = false;
    }

    /**
     * If the given point is on the object, the object will be selected
     * @return True if the object has been selected
     */
    public boolean doSelected(int x, int y) {
        int count = 0;
        double d = 0.0;
        
        d = start.distance(x/scaleX, y/scaleY);
        if ( d<= 3)
        {
            selected = true;
            draggedPoint = 0;
            return true;
        }

        d = end.distance(x/scaleX, y/scaleY);
        if ( d<= 3)
        {
            selected = true;
            draggedPoint = 1;
            return true;
        }
        
        draggedPoint = -1;
        return false;
    }

    /**
     * 
     * @return Returns true if this object is selected
     */
    public boolean isSelected() {
        return selected;
    }

    /**
     * Drag the nearby point
     */
    public void doDragged(int x, int y) {
        double d;
    
        if(draggedPoint == -1) {
            d = start.distance(x/scaleX, y/scaleY);
            if ( d<= 3)
            {
                selected = true;
                draggedPoint = 0;
            }

            d = end.distance(x/scaleX, y/scaleY);
            if ( d<= 3)
            {
                selected = true;
                draggedPoint = 1;
            }
        }

        if(draggedPoint == 0) {
            start.setLocation(x/scaleX, y/scaleY);
        }
        
        if(draggedPoint == 1) {
            end.setLocation(x/scaleX, y/scaleY);
        }
    
    }

    /**
     * Called when the mouse is released and this object was selected
     */
    public void doReleased() {
        draggedPoint = -1;
    }


    /**
     * Selected ?
     * @return Returns true if this object is complete and can be added into scene
     */
    public boolean isComplete() {
        if(start != null && end != null) return true;
        return false;
    }

    /**
     * Set this object as selected
     */
    public void select() {
        selected = true;
    }

    /**
     * Adds a point into the object
     * @param i x-axis in pixels of the point to be added
     * @param i0 y-axis in pixels of the point to be added
     */
    public void addPoint(int i, int i0) {
        addPoint(new Point2D.Double(i/scaleX, i0/scaleY));
    }

    /**
     * Finds out if a point can be added to the object
     * @return true if can add, false otherwise
     */
    public boolean canAdd() {
        return !isComplete();
    }

    /**
     * Sets ObjectInfo class
     * @param i ObjectInfo class
     */
    public void setInfo(ObjectInfo i) {
        info = i;
    }

    /**
     * Get info
     * @return Returns reference to ObjectInfo class
     */
    public ObjectInfo getInfo() {
        return info;
    }
 
    // MDSYS.SDO_GEOMETRY(2002, NULL, NULL, MDSYS.SDO_ELEM_INFO_ARRAY(1,2,1),
    // MDSYS.SDO_ORDINATE_ARRAY(24,58, 44,99))         
    
    /**
     * Returns String with SDO_GEOM syntax to be inserted into DB
     * @return String with SDO syntax
     */
    public String getGeom() {
        String s = "MDSYS.SDO_GEOMETRY(2002, NULL, NULL, MDSYS.SDO_ELEM_INFO_ARRAY(1,2,1),MDSYS.SDO_ORDINATE_ARRAY(";
        
        s += String.valueOf(start.getX());
        s+=',';
        s += String.valueOf(start.getY());
        s+=',';
        s += String.valueOf(end.getX());
        s+=',';
        s += String.valueOf(end.getY());

        s+="))";
        return s;
    }    

    
}
